package bb.lanxing.activity;

import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.databinding.ActivityCadenceSectionSettingBinding;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.User;
import bb.lanxing.model.json.UserSettings;
import bb.lanxing.mvvm.base.BaseDBActivity;
import bb.lanxing.mvvm.sportsetting.SportSettingViewModel;
import bb.lanxing.mvvm.sportsetting.UserSportSetting;
import bb.lanxing.util.Log;
import bb.lanxing.util.SectionParseUtil;
import bb.lanxing.util.ui.InputHelper;
import bb.lanxing.view.BiciAlertDialogBuilder;
import org.apache.commons.cli.HelpFormatter;

public class CadenceSectionSettingActivity extends BaseDBActivity<ActivityCadenceSectionSettingBinding> {
    final private static String TAG = "CadenceSectionSettingActivity";
    ActivityCadenceSectionSettingBinding cadenceSectionSettingBinding;
    private int[] sections = null;
    SportSettingViewModel viewModel = new SportSettingViewModel(App.getContext());


    @Override
    public int getLayoutId() {
        return R.layout.activity_cadence_section_setting;
    }

    @Override
    public void initView(ActivityCadenceSectionSettingBinding binding) {
        this.cadenceSectionSettingBinding = binding;
        setupActionBar(true);
        initData(getIntent().getStringExtra(DeviceSettingsActivity.EXTRA_CADENCE_SEGMENT));
        initView();
    }

    private void initView() {
//        cadenceSectionSettingBinding.z1ValueRange.setOnClickListener(v -> showEditDialog(0, String.valueOf(sections[0])));
        cadenceSectionSettingBinding.z2ValueRange.setOnClickListener(v -> showEditDialog(1, String.valueOf(sections[1])));
        cadenceSectionSettingBinding.z3ValueRange.setOnClickListener(v -> showEditDialog(2, String.valueOf(sections[2])));
        cadenceSectionSettingBinding.z4ValueRange.setOnClickListener(v -> showEditDialog(3, String.valueOf(sections[3])));
        updateSectionView();
    }

    public void updateSectionView() {
        int[] iArr = {175, 176, 177, 178};//not used
        for (int length = this.sections.length - 1; length >= 0; length--) {
            int[] iArr2 = this.sections;
            int i = iArr2[length];
            if (length == 0) {
                this.cadenceSectionSettingBinding.z1ValueRange.setText(getString(R.string.mine_section_setting_ca_lt_with_unit, String.valueOf(iArr2[length + 1])));
            } else if (length == 3) {
                this.cadenceSectionSettingBinding.z4ValueRange.setText(getString(R.string.mine_section_setting_ca_gt_with_unit, String.valueOf(i)));
            } else {
                String string = getString(R.string.mine_section_setting_ca_value, String.valueOf(iArr2[length + 1]), String.valueOf(this.sections[length]));
                int indexOf = string.indexOf(HelpFormatter.DEFAULT_OPT_PREFIX);
                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.grey_999999));
                ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.grey_666666));
                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
                spannableStringBuilder.setSpan(foregroundColorSpan, 0, indexOf, 33);
                spannableStringBuilder.setSpan(foregroundColorSpan2, indexOf + 1, string.length(), 33);
                if (length == 1) {
                    this.cadenceSectionSettingBinding.z2ValueRange.setText(spannableStringBuilder);
                } else if (length == 2) {
                    this.cadenceSectionSettingBinding.z3ValueRange.setText(spannableStringBuilder);
                }
            }
        }
    }

    private void initData(String cadenceSagment) {
        UserSettings userSettings;
        if (cadenceSagment == null || cadenceSagment.isEmpty()) {
            cadenceSagment = SharedManager.getInstance().getCadenceSectionWithMP();
        }
        int[] parse = SectionParseUtil.parse(cadenceSagment, 4);
        this.sections = parse;
        if (parse == null || parse.length == 0) {
            User signinUser = App.getContext().getSigninUser();
            if (signinUser != null && (userSettings = signinUser.getUserSettings()) != null) {
                this.sections = userSettings.getCadenceSections();
            }
            if (this.sections == null) {
                this.sections = new int[]{0, 80, 95, 160};
            }
        }
    }

    public void showEditDialog(final int i, final String str) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.edittext_view, (ViewGroup) null);
        final EditText editText = inflate.findViewById(R.id.editText);
        editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS | InputType.TYPE_CLASS_NUMBER);
        editText.setMaxLines(1);
        if (TextUtils.isEmpty(str)) {
            editText.setHint(R.string.dialog_hint_input);
        } else {
            editText.setText(str);
        }

        AlertDialog.Builder mNegativeButton =
                new BiciAlertDialogBuilder(this)
                        .setView(inflate)
                        .setPositiveButton(R.string.dialog_btn_ok, (dialogInterface, i2) -> {
                            String obj = editText.getText().toString();
                            if (TextUtils.isEmpty(obj) || obj.equals(str)) {
                                dialogInterface.cancel();
                            } else {
                                updateSections(i, Math.abs(Integer.parseInt(obj)));
                                String makeSectionString = makeSectionString(sections);
                                SharedManager.getInstance().setCadenceSectionWithMP(makeSectionString);
                                updateSectionView();
                                requestSetting(makeSectionString);
                            }
                            InputHelper.hideSoftInput(editText);
                        })
                        .setNegativeButton(R.string.dialog_btn_cancel, (dialogInterface, i2) -> InputHelper.hideSoftInput(editText));
        mNegativeButton.setTitle(R.string.mine_section_setting_dialog_input);
        mNegativeButton.show();
        InputHelper.showSoftInput(editText);
    }

    public void updateSections(int index, int section) {
        if (this.sections == null) {
            return;
        }
        Log.v(TAG, "updateSections, index = " + index + ", section = " + section);
        int[] iArr = this.sections;
        int i = index == iArr.length + (-1) ? 256 : iArr[index + 1];
        int i2 = index == 0 ? 0 : iArr[index - 1];
        if (section >= i) {
            section = i - 1;
        } else if (section <= i2) {
            section = i2 + 1;
        }
        iArr[index] = section;
    }

    public String makeSectionString(int[] sections) {
        String str = "";
        for (int i = 0; i < sections.length; i++) {
            str = str + sections[i];
            if (i < sections.length - 1) {
                str = str + ";";
            }
        }
        Log.v(TAG, "makeSectionString, jsonStr = " + str);
        return str;
    }

    public void requestSetting(String jsonStr) {
        this.viewModel.updateSportSetting(App.getContext().getUserId(), new UserSportSetting(null, null, null, null, null, jsonStr));
    }
}
